<?php

namespace app\api\controller\xiservice;

use app\common\controller\xiservice\XiserviceApi;

/**
 * 分润模块
 */
class Dividend extends XiserviceApi
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * @ApiTitle (用户分润列表)
     * @ApiMethod (GET)
     * @ApiRoute (/api/xiservice.dividend/list)
     * @ApiHeaders (name=token, type=string, description="Token")
     * @ApiParams  (name="page", type="integer", description="页码")
     * @ApiParams  (name="rows", type="integer", description="页数")
     * @ApiParams  (name="type", type="string", description="订单类型:member=包年月订单,service=服务订单")
     *
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="返回数据")
     */
    public function list()
    {
        $user_id = $this->auth->id;
        $limit = $this->request->param('rows/d', 10);
        $type = $this->request->param('order_type/s', '');

        $lists = \app\common\model\xiservice\Dividend::field('id,order_type,source_order_no as order_no,divide_money as money,createtime')
            ->where('user_id', $user_id)
            ->where(function ($query) use ($type) {
                if ($type) {
                    $query->where('order_type', $type);
                }
            })
            ->order('id', 'desc')
            ->paginate(10);

        $this->success('ok', $lists);
    }

    /**
     * @ApiTitle (业务员提成统计)
     * @ApiMethod (GET)
     * @ApiRoute (/api/xiservice.dividend/census)
     * @ApiHeaders (name=token, type=string, description="Token")
     *
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="返回数据")
     */
    public function census()
    {
        $user_id = $this->auth->id;

        // - 验证用户是否业务员
        $service_user_info = \app\common\model\xiservice\User::where('user_id', $user_id)->find();
        $res_data = [
            'member_commission_total' => 0,
            'member_commission_count' => 0,
            'service_commission_total' => 0,
            'service_commission_count' => 0,
            'commission_total' => 0,
            'can_withdrawal_total' => 0,
        ];
        if (empty($service_user_info)) {
            $this->error('获取不到用户信息', $res_data);
        } else if ($service_user_info['group_id'] != 4) {
            $this->error('该用户不是业务员', $res_data);
        }

        // 会员升级订单
        $condition_member = [
            'user_id' => $user_id,
            'order_type' => 'member',
        ];
        $member_commission_total = \app\common\model\xiservice\Dividend::where($condition_member)->sum('divide_money');
        $member_commission_count = \app\common\model\xiservice\Dividend::where($condition_member)->count();

        // 服务订单
        $condition_service = [
            'user_id' => $user_id,
            'order_type' => 'service',
        ];
        $service_commission_total = \app\common\model\xiservice\Dividend::where($condition_service)->sum('divide_money');
        $service_commission_count = \app\common\model\xiservice\Dividend::where($condition_service)->count();

        $res_data = [
            'member_commission_total' => $member_commission_total,
            'member_commission_count' => $member_commission_count,
            'service_commission_total' => $service_commission_total,
            'service_commission_count' => $service_commission_count,
            'commission_total' => $member_commission_total + $service_commission_total,
            'can_withdrawal_total' => empty($service_user_info['can_money']) ? 0 : $service_user_info['can_money'],
        ];

        $this->success('查询成功', $res_data);
    }

    /**
     * 团队列表
     */
    public function below()
    {
        $uid = $this->auth->id;

        $lists = \app\common\model\User::where('pid', $uid)
            ->field('id,nickname,avatar,jointime')
            ->order('jointime', 'desc')
            ->paginate();

        $this->success('ok', $lists);
    }
}